Redis cluster实现master水平扩容

发布 : 2017-06-28 分类 : 大数据 浏览 :
1
2
3
192.168.31.231 matrix-cache01
192.168.31.232 matrix-cache02
192.168.31.233 matrix-cache03
1
2
3
4
5
6
7
8
9
redis cluster模式下,不建议做物理的读写分离了

建议通过master的水平扩容,来横向扩展读写吞吐量,还有支撑更多的海量数据

redis单机,读吞吐是5w/s,写吞吐2w/s

扩展redis更多master,那么如果有5台master,不就读吞吐可以达到总量25/s QPS,写可以达到10w/s QPS

扩容到5台master,能支撑的总的缓存数据量就是30G,40G -> 100台,600G,800G,甚至1T+,海量数据

redis扩容

加入新master

1
[root@matrix-cache03 ~]# mkdir -p /var/redis/7007
1
[root@matrix-cache03 ~]# cd /etc/redis
1
[root@matrix-cache03 redis]# vi 7007.conf
1
2
3
4
5
6
7
8
9
10
port 7007
cluster-enabled yes
cluster-config-file /etc/redis-cluster/node-7007.conf
cluster-node-timeout 15000
daemonize yes
pidfile /var/run/redis_7007.pid
dir /var/redis/7007
logfile /var/log/redis/7007.log
bind 192.168.31.233
appendonly yes

Markdown

1
2
[root@matrix-cache03 ~]# cd /etc/init.d
[root@matrix-cache03 init.d]# cp redis_6379 redis_7007
1
2
[root@matrix-cache03 init.d]# vi redis_7007
REDISPORT=7007

Markdown

启动新master

1
2
3
[root@matrix-cache03 init.d]# ./redis_7007 start
Starting Redis server...
[root@matrix-cache03 init.d]# cat /var/log/redis/7007.log

Markdown

在集群中加入新master

1
2
[root@matrix-cache01 ~]# cd /usr/local
[root@matrix-cache01 local]# redis-trib.rb add-node 192.168.31.233:7007 192.168.31.231:7001

Markdown

1
[root@matrix-cache01 local]# redis-trib.rb check 192.168.31.231:7001

Markdown

1
连接到新的redis实例上,cluster nodes,确认自己是否加入了集群,作为了一个新的master
1
2
3
4
5
6
7
8
9
10
[root@matrix-cache03 ~]# redis-cli -h 192.168.31.233 -p 7007
192.168.31.233:7007> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

reshard一些数据过去

1
resharding的意思就是把一部分hash slot从一些node上迁移到另外一些node上
1
2
3
4
5
6
7
要把之前3个master上,总共4096个hashslot迁移到新的第四个master上去

How many slots do you want to move (from 1 to 16384)?
16384/3=5462
16384/4=4096

16384-4096*3
1
[root@matrix-cache01 local]# redis-trib.rb reshard 192.168.31.231:7001

Markdown

Markdown

1
2
3
输入完ID后
Source node #4:done
即开始将源数据分配到7007

查看7007是否分配到了数据

1
[root@matrix-cache01 local]# redis-trib.rb check 192.168.31.231:7001

Markdown

添加node作为slave

1
[root@matrix-cache03 ~]# mkdir -p /var/redis/7008
1
2
[root@matrix-cache03 ~]# cd /etc/redis
[root@matrix-cache03 redis]# vi 7008.conf
1
2
3
4
5
6
7
8
9
10
port 7008
cluster-enabled yes
cluster-config-file /etc/redis-cluster/node-7008.conf
cluster-node-timeout 15000
daemonize yes
pidfile /var/run/redis_7008.pid
dir /var/redis/7008
logfile /var/log/redis/7008.log
bind 192.168.31.233
appendonly yes

Markdown

1
2
[root@matrix-cache03 ~]# cd /etc/init.d
[root@matrix-cache03 init.d]# cp redis_6379 redis_7008
1
2
[root@matrix-cache03 init.d]# vi redis_7008
REDISPORT=7008

Markdown

1
2
3
[root@matrix-cache03 init.d]# ./redis_7008 start
Starting Redis server...
[root@matrix-cache03 init.d]# cat /var/log/redis/7008.log

Markdown

1
[root@matrix-cache01 local]# redis-trib.rb add-node --slave --master-id e2498a3792ee21cd70736fead0d7f3157ded2f0d 192.168.31.233:7008 192.168.31.231:7001

Markdown

Markdown

删除node

1
先用resharding将数据都移除到其他节点,确保node为空之后,才能执行remove操作
1
[root@matrix-cache01 local]# redis-trib.rb reshard 192.168.31.231:7001

Markdown

Markdown

1
2
3
4
5
6
将7007的4096 slots数据移动到别的节点
2个是1365 slots,1个是1366 slots

当你清空了一个master的hashslot时,redis cluster就会自动将其slave挂载到其他master上去

这个时候就只要删除掉master就可以了
1
2
3
4
[root@matrix-cache01 local]# redis-trib.rb del-node 192.168.31.231:7001 e2498a3792ee21cd70736fead0d7f3157ded2f0d
>>> Removing node e2498a3792ee21cd70736fead0d7f3157ded2f0d from cluster 192.168.31.231:7001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

Markdown

1
[root@matrix-cache01 local]# redis-trib.rb check 192.168.31.231:7001

Markdown

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/06/28/Redis cluster实现master水平扩容/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹